package com.du.maven.email.impl;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import com.du.maven.pojo.MailAuthenticator;

public class AccountEmailServiceImpl {
	private Session session;
	private String systemEmail;
	private String password;
	private String smtpHost;
	private Authenticator authenticator;
	private String auth;
	private boolean isInit = false;
	
	public void sendmailHtmlText(String to, String subject, String htmlText) {
		initSession();
		try {
			// 创建mime类型邮件
			final MimeMessage message = new MimeMessage(session);
			// 设置发信人
			message.setFrom(new InternetAddress(systemEmail));
			// 设置收件人
			message.setRecipient(RecipientType.TO, new InternetAddress(to));
			// 设置主题
			message.setSubject(subject);
			// 设置邮件内容
			message.setContent(htmlText, "text/html;charset=utf-8");
			// 发送
			Transport.send(message);
		} catch (Exception e) {
			System.out.println("发送失败");
			throw new RuntimeException("发送失败");
		}
	}

	public void sendmailText(String to, String subject, String text){
		initSession();
		try{
			// 创建mime类型邮件
			final MimeMessage message = new MimeMessage(session);
			// 设置发信人
			message.setFrom(new InternetAddress(systemEmail));
			// 设置收件人
			message.setRecipient(RecipientType.TO, new InternetAddress(to));
			// 设置主题
			message.setSubject(subject);
			// 设置邮件内容
			message.setContent(text, "text/plain;charset=utf-8");
			// 发送
			Transport.send(message);
		}catch(Exception e){
			System.out.println("发送失败");
			throw new RuntimeException("发送失败");
		}
	}
	public void sendmailHtmlText2Many(String[] tos, String subject, String htmlText) {
		initSession();
		try {
			// 创建mime类型邮件
			final MimeMessage message = new MimeMessage(session);
			// 设置发信人
			message.setFrom(new InternetAddress(systemEmail));
			// 设置收件人群
			InternetAddress addresses[] = new InternetAddress[tos.length];
			for(int  i = 0 ; i < tos.length;i++){
				addresses[i] = new InternetAddress(tos[i]);
			}
			message.setRecipients(RecipientType.TO,addresses);
			// 设置主题
			message.setSubject(subject);
			// 设置邮件内容
			message.setContent(htmlText, "text/html;charset=utf-8");
			// 发送
			Transport.send(message);
		} catch (Exception e) {
			System.out.println("发送失败");
			throw new RuntimeException("发送失败");
		}
	}
	public void sendmailText2Many(String[] tos, String subject, String Text) {
		initSession();
		try {
			// 创建mime类型邮件
			final MimeMessage message = new MimeMessage(session);
			// 设置发信人
			message.setFrom(new InternetAddress(systemEmail));
			// 设置收件人群
			InternetAddress addresses[] = new InternetAddress[tos.length];
			for(int  i = 0 ; i < tos.length;i++){
				addresses[i] = new InternetAddress(tos[i]);
			}
			message.setRecipients(RecipientType.TO,addresses);
			// 设置主题
			message.setSubject(subject);
			// 设置邮件内容
			message.setContent(Text, "text/plain;charset=utf-8");
			// 发送
			Transport.send(message);
		} catch (Exception e) {
			System.out.println("发送失败");
			throw new RuntimeException("发送失败");
		}
	}
	private void initSession() {
		if(isInit){
			return;
		}
		Properties props = System.getProperties();
		props.put("mail.smtp.host", smtpHost + "." + systemEmail.split("@")[1]);
		props.put("mail.smtp.auth", auth);
		authenticator = new MailAuthenticator(systemEmail, password);
		session = Session.getInstance(props, authenticator);
		isInit = true;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public String getSystemEmail() {
		return systemEmail;
	}

	public void setSystemEmail(String systemEmail) {
		this.systemEmail = systemEmail;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSmtpHost() {
		return smtpHost;
	}

	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}

	public String getAuth() {
		return auth;
	}

	public void setAuth(String auth) {
		this.auth = auth;
	}

}
